
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "list.h"


struct favor_item {
	struct list_head list;
	char name[64];
};

static LIST_HEAD(favor_list);

static void favor_add(struct favor_item *item)
{
	struct favor_item *p;
	p = malloc(sizeof(*p));
	memcpy(p, item, sizeof(*item));
	list_add(&p->list, &favor_list);
}

static void favor_del(struct favor_item *item)
{
	struct favor_item *f = NULL;
	list_for_each_entry(f, &favor_list, list){
		if(strcmp(f->name, item->name)==0){
			break;
		}
	}
	if(f){
		list_del(&f->list);
		free(f);
	}
}

static void favor_remove(int n)
{
	struct favor_item *f = NULL;
	int i=0;
	list_for_each_entry(f, &favor_list, list){
		if(i++>=n) break;
	}
	if(f){
		list_del(&f->list);
		free(f);
	}
}


static void favor_show(void)
{
	int i=0;
	struct favor_item *f;
	struct list_head *p;
	list_for_each_entry(f,&favor_list,list){
		printf("i=%d, %s\n", i++, f->name);
	}
	// list_for_each(p, &favor_list){
	// 	f = list_entry(p,struct favor_item,list);
	// 	printf("i=%d, %s\n", i++, f->name);
	// }
}

int main(int ac, char *av)
{

	char op[8];
	int val = 0;
	int i;
	char* sample[] = {
		"one", "two", "three","four", "five", "six", "seven", "eight", "night", "ten"
	};

	struct favor_item item;
	for(i=0; i<sizeof(sample)/sizeof(sample[0]); i++){
		strcpy(item.name, sample[i]);
		favor_add(&item);
	}
	while(1){
		scanf("%s %s", op, item.name);
		switch(op[0]){
			case 'a':
				favor_add(&item);
				break;
			case 'd':
				favor_del(&item);
				break;
			default:
				if(isdigit(op[0])){
					val = atoi(op);
					favor_remove(val);
				}
				break;
		}

		favor_show();
	}

}

